﻿

$(document).ready(function () {
    //初始化表单验证
    initValidate();

    if ($("#Id").val() > 0) {
        $("#Username").attr("disabled", true);
        $("#Email").attr("disabled", true);
    }
   
    //保存
    $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
    //取消
    $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });
});

//数据验证
function initValidate() {
    //表单验证
    $('#inputform').validate({
        rules: {
            Username: {
                required: true,
                alnum: true,
                minlength: 2,
                maxlength: 50,
                remote: {
                    url: baseurl + 'Member/UsernameCheck',
                    type: 'post',
                    data: {},
                    dataFilter: function (data, type) {
                        if (data == "0")
                            return true;
                        else
                            return false;
                    }
                }
            },
            Name: {
                required: true,
                maxlength: 200
            },
            Email: {
                required: true,
                maxlength: 200,
                eduEmail: true,
                remote: {
                    url: baseurl + 'Member/EmailCheck',
                    type: 'post',
                    data: {},
                    dataFilter: function (data, type) {
                        if (data == "0")
                            return true;
                        else
                            return false;
                    }
                }
            },
            Mobile: { required: true, maxlength: 200, isMobile: true },
            Membertype: { required: true, maxlength: 200 },
            School: { required: true, maxlength: 200 },
            Specialty: { required: true, maxlength: 200 },
            Address: { required: true, maxlength: 200 }
        },
        errorPlacement: function (error, element) {
            if (element.attr("type") == "radio") {
                error.appendTo(element.parent().parent());
            } else {
                error.appendTo(element.parent());
            }
        },
        messages: {
            Username: {
                required: '请输入用户名',
                minlength: '用户名不能少于2个字符',
                maxlength: '用户名不能超过50个字符',
                remote: '该用户名已被占用'
            },
            Password: {
                required: '请输入密码',
                minlength: '密码不能少于6个字符',
                maxlength: '密码不能超过16个字符'
            },
            ConfirmPassword: {
                equalTo: '两次输入的密码不一致'
            },
            Name: {
                required: '请输入姓名',
                maxlength: '学校名称不能超过20个字'
            },
            Mobile: {
                required: '请输入手机号',
                isMobile: '请输入正确的手机号'
            },
            Email: {
                required: '请输入邮箱地址',
                remote: '该邮箱已被使用'
            },
            Membertype: {
                required: '请选择身份'
            },
            School: {
                required: '请输入学校名称',
                maxlength: '学校名称不能超过200个字'
            },
            Specialty: {
                required: '请输入所属院系',
                maxlength: '所属院系不能超过200个字'
            },
            Address: {
                required: '请输入通信地址',
                maxlength: '通信地址不能超过200个字'
            }
        }
    });
}

//保存数据
function save(obj) {
   // debugger;
    if (!$("#inputform").valid()) {
        return;
    }

    var jsondata = {
        Id: $("#Id").val(),
        Values: {}
    };
    ////记录ID
    //jsondata.Id = $("#Id").val();
    //主表数据
    $("#inputform .datafield").each(function () {
        jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
    });

    //禁用保存按钮，防止重复提交
    $(obj).attr("disabled", true);

    $.ajax({
        type: "post",
        url: baseurl + "Member/Save",
        data: {
            json: JSON.stringify(jsondata)
        },
        success: function (e) {
            $(obj).attr("disabled", false);
            if (e.Ret == "0") {
                parent.closeDialog(true);
            } else {
                $.messager.alert("错误", e.Msg, "error");
            }
        },
        error: function (xhr) {
            $(obj).attr("disabled", false);

            showError(xhr);
        }
    });
}